// import {ArrayUtils} from "../utils/ArrayUtils";

import {ArrayUtils} from 'ebom/utils/ArrayUtils.js'
import {DrawingUtils} from 'ebom/drawing/DrawingUtils.js'
import {TableConstants} from "../constant/Constants";


export const PartUtils = {

    // 零件主对象表
    partMasterTable: 'partMaster',
    // 零件版次表
    partIterationTable: 'partIteration',
    // 零件使用关系表
    partUsageLinkTable: 'partUsageLink',
    // 零件结构表
    partStructureTable: 'partStructure',

    // 零件结构表 子对象
    partStructureChildObjField: 'childObject',

    /**
     * 添加零件到零件结构
     * @param selectPart 选中的零件 零件结构表
     * @param number 零件编号
     * @param name 零件名称
     * @param quantity 数量
     */
    addPart2Bom: function (selectPart, config) {
        const partInfo = this.createPartUseConfig(config);

        let newMasterId = partInfo.masterId;
        let newIterationId = partInfo.iterationId;
        let bomRecordId = this.createPartStructure(newMasterId, newIterationId);

        this.createPartUsageLinkUpdateStructure(selectPart.iterationRef, newMasterId, config.quantity, bomRecordId, selectPart.id);
    },


    /**
     * 根据编号查询是否存在零件
     * @param number 零件编号
     * @returns {boolean}
     */
    checkCanCreatePart: function (number) {
        let result = informat.table.queryList(this.partMasterTable, {
            filter: {
                opt: 'and',
                conditionList: [
                    {fieldId: 'number', opt: 'eq', value: number},
                ],
            }
        });
        return result.length < 1;
    },

    createPartUseConfig: function (config) {
        let phase = 'C';
        if (config.phase) {
            phase = config.phase;
        }
        let partType = 'ZZJ';
        if (config.partType) {
            partType = config.partType;
        }
        let partClass = 'YBJ';
        if (config.partClass) {
            partClass = config.partClass;
        }
        let isEndItem = false;
        if (config.isEndItem) {
            isEndItem = config.isEndItem;
        }
        return this.createPart(
            config.number,
            config.name,
            phase,
            partType,
            partClass,
            isEndItem,
        );
    },

    /**
     * 创建零件
     * @param number 零件编号
     * @param name 零件名称
     * @param phase 阶段
     * @returns {{masterId: 零件主对象id, iterationId: 零件版次id}}
     */
    createPart: function (number, name, phase, partType, partClass, isEndItem) {
        // 1.创建主对象
        const masterId = this.getPartMasterId(number, name, phase, partType, partClass, isEndItem);

        // 2.创建版次对象
        const iterationId = this.createPartIteration(masterId, phase, 1, true);

        console.log('masterId:', masterId);
        console.log('iterationId:', iterationId);


        // 5. 添加关联列表
        informat.table.addRelation(this.partMasterTable, 'iterationList', masterId, iterationId);

        let drawingInfo = DrawingUtils.createDrawing(masterId, iterationId);

        // 4.更新记录
        informat.table.update(this.partMasterTable, {
            'id': masterId,
            'iterationId': iterationId,
            'drawingMasterId': drawingInfo.masterId,
        });

        // 4.更新记录
        informat.table.update(this.partIterationTable, {
            'id': iterationId,
            'drawingIterationId': drawingInfo.iterationId,
        });

        return {
            masterId: masterId,
            iterationId: iterationId,
            drawingMasterId: drawingInfo.masterId,
            drawingIterationId: drawingInfo.iterationId,
        };
    },

    getPartMasterId: function (number, name, phase, partType, partClass, isEndItem) {
        let masterId = '';
        let result = informat.table.queryList(TableConstants.partMaster, {
            filter: {
                opt: 'and',
                conditionList: [
                    {fieldId: 'number', opt: 'eq', value: number},
                ],
            }
        });
        if (ArrayUtils.isEmpty(result)) {
            masterId = informat.table.insert(TableConstants.partMaster, {
                'number': number,
                'name': name,
                'partType': partType,
                'partClass': partClass,
                'isEndItem': isEndItem,
            })
        } else {
            masterId = result[0].id;
        }
        return masterId;
    },

    createPartIteration: function (masterId, phase, iteration, isLatestIteration) {
        return informat.table.insert(this.partIterationTable, {
            'primaryObject': masterId,
            'phase': phase,
            'iteration': iteration,
            'isLatestIteration': isLatestIteration,
        })
    },

    /**
     * 创建零件结构记录
     * @param masterId 零件主对象id 零件版次表
     * @param iterationId 零件版次id 零件主对象
     * @returns {string}
     */
    createPartStructure: function (masterId, iterationId) {
        // 3.创建BOM表记录
        const bomRecordId = informat.table.insert(this.partStructureTable, {
            'masterRef': masterId,
            'iterationRef': iterationId,
        });
        return bomRecordId;
    },

    /**
     * 创建零件使用关系
     * @param parentId 父节点id 零件版次表
     * @param childId 子节点id 零件主对象
     * @param quantity 使用数量
     * @returns {string}
     */
    createPartUsageLink: function (parentId, childId, quantity) {
        let linkId = '';
        let result = this.queryPartUsageLinkById(parentId, childId);
        if (ArrayUtils.isEmpty(result)) {

            linkId = informat.table.insert(this.partUsageLinkTable, {
                'parentId': parentId,
                'childId': childId,
                'quantity': quantity,
            });
        } else {
            linkId = result[0].id;
        }

        return linkId;
    },

    /**
     * 创建零件使用关系 并更新零件结构
     *
     * @param parentId 父节点id 零件版次表
     * @param childId 子节点id 零件主对象
     * @param quantity 使用数量
     * @param childBomId 子节点结构id 零件结构表
     * @param parentBomId 父节点结构id 零件结构表
     * @returns {string} 零件使用关系id
     */
    createPartUsageLinkUpdateStructure: function (parentId, childId, quantity, childBomId, parentBomId) {
        const linkId = this.createPartUsageLink(parentId, childId, quantity);
        informat.table.update(this.partStructureTable, {
            'id': childBomId,
            'usagelinkRef': linkId,
        });

        informat.table.updateChildrenField(this.partStructureTable, this.partStructureChildObjField, childBomId, parentBomId);
        return linkId;
    },

    /**
     * 根据零件编号查询零件
     * @param number 零件编号
     * @returns {Array<RowData>}
     */
    queryPartByNumber: function (number) {
        return informat.table.queryList(this.partMasterTable, {
            filter: {
                opt: 'and',
                conditionList: [
                    {fieldId: 'number', opt: 'eq', value: number},
                ],
            }
        })
    },

    /**
     * 根据 父子 id 查询零件使用关系
     * @param parentId 父节点id 零件版次表
     * @param childId 子节点id 零件主对象
     * @returns {Array<RowData>}
     */
    queryPartUsageLinkById: function (parentId, childId) {
        return informat.table.queryList(this.partUsageLinkTable, {
            filter: {
                opt: 'and',
                conditionList: [
                    {fieldId: 'parentId', opt: 'eq', value: parentId},
                    {fieldId: 'childId', opt: 'eq', value: childId},
                ],
            }
        })
    }

}